<?php
namespace Hilaoyu\LaravelExtend\Supports;
/**
 * Created by laoyu.
 * User: hilaoyu@qq.com
 * Date: 2018/6/5
 * Time: 11:03
 */

use Endroid\QrCode\ErrorCorrectionLevel;
use Hilaoyu\LaravelExtend\Services\Files\FileService;
use Zxing\QrReader;


class QrCode extends \Endroid\QrCode\QrCode
{
    protected $save_disk = 'temp';

    /**
     * @param string $content 二维码内容
     * @param string $logo 中间logo文件的绝对地址
     * @param string $color 二维码颜色
     * @param string $bgColor 二维背景颜色
     * @param int $size 二维码大小
     * @return static
     */
    static public function make($content = '',$logo='',$color="#000000",$bgColor="#FFFFFF",$size=300){
        $logo = trim($logo);
        $qrCode = new static($content);
        $qrCode->setSize($size);

// Set advanced options
        $qrCode->setWriterByName('png');
        $qrCode->setMargin(10);
        $qrCode->setEncoding('UTF-8');
        $qrCode->setErrorCorrectionLevel(new ErrorCorrectionLevel(ErrorCorrectionLevel::HIGH));
        $qrCode->setForegroundColor(static::colorHexToRgb($color));
        $qrCode->setBackgroundColor(static::colorHexToRgb($bgColor));
        if($logo && file_exists($logo)){
            $qrCode->setLogoPath($logo);
        }

        $qrCode->setLogoWidth($size/5);
        $qrCode->setRoundBlockSize(true);
        $qrCode->setValidateResult(false);

        return $qrCode;
    }

    public function setStorageDisk($disk){
        $disk = trim($disk);
        if(!empty($disk)){
            $this->save_disk = $disk;
        }
        return $this;
    }

    public function response( ){
        header('Content-Type: '.$this->getContentType());
        echo $this->writeString();
        exit;
    }

    public function save($path= null,$name=null){
        $ext = is_null($name) ? 'png' : null;
        $path = FileService::generateHashPath($path,$name,$ext);
        if(! $storage = FileService::makeStorage($this->save_disk)){
            return false;
        }

        $path_real = $storage->path($path);

        $dir = dirname($path_real);

        if((!is_dir($dir) && ! mkdir($dir,0777,true))
            || !is_writable($dir)
        ){
            return false;
        }

        $this->writeFile($path_real);
        if($storage->exists($path)){
            return new FileService($this->save_disk,$path);
            //return FileService::buildFsPath($this->save_disk,$path);
        }
        return false;
    }

    static protected function colorHexToRgb( $color ) {
        if(is_array($color)){
            return $color;
        }
        if ( $color[0] == '#' ) {
            $color = substr( $color, 1 );
        }
        if ( strlen( $color ) == 6 ) {
            list( $r, $g, $b ) = array( $color[0] . $color[1], $color[2] . $color[3], $color[4] . $color[5] );
        } elseif ( strlen( $color ) == 3 ) {
            list( $r, $g, $b ) = array( $color[0] . $color[0], $color[1] . $color[1], $color[2] . $color[2] );
        } else {
            return false;
        }
        $r = hexdec( $r );
        $g = hexdec( $g );
        $b = hexdec( $b );
        return array( 'r' => $r, 'g' => $g, 'b' => $b , 'a' => 0);
    }

    /**
     * @param $imgSource
     * @param string $sourceType
     * @param bool $useImagickIfAvailable
     * @return QrReader
     */
    static public function makeReader($imgSource, $sourceType = QrReader::SOURCE_TYPE_FILE, $useImagickIfAvailable = true){
        return new QrReader($imgSource, $sourceType,$useImagickIfAvailable);
    }
}